package com.home.duy.GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;

import javax.swing.JPanel;
import javax.swing.JInternalFrame;

import com.home.Common.Resources_Duy;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import javax.swing.JScrollPane;
import javax.swing.JTable;

import com.home.Validation.Validator;
import com.home.duy.code.DAO.BanDAO;
import com.home.duy.code.DAO.NhanVienDAO;
import com.home.duy.code.FileFilter.ImageFileFilter;
import com.home.duy.code.Model.QLBanTableModel;
import com.home.duy.code.Model.QLNVTableModel;
import com.home.duy.code.Renderer.CurrencyTableCellRenderer;
import com.home.duy.code.components.ImageFileView;
import com.home.duy.code.components.ImagePanel;
import com.home.duy.code.components.JTextFieldCurrency;
import com.home.duy.code.components.NumbericTextField;

import javax.swing.JButton;
import com.home.Common.Common;
import com.home.Entity.Ban;
import com.home.Entity.NhanVien;

import javax.swing.ImageIcon;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.RowFilter;
import javax.swing.SwingConstants;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.TableModelEvent;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableRowSorter;
import java.awt.GridBagLayout;
import java.io.File;

import javax.swing.JTextArea;

public class QuanLyNV extends JInternalFrame {

	private static final int MAX_NUMBER_PER_PAGE = 10;

	private QLNVTableModel pagingModel = null;
	private int indexPos = 0;
	private TableRowSorter<QLNVTableModel> sorter = null;
	private NhanVien nvSelected = null;
	private BufferedImage imgCurrent = null;
	private String extension = "";

	private JPanel jContentPane = null;
	private JPanel panelDuLieu = null;
	private JScrollPane scrollPaneTable = null;
	private JTable tableData = null;
	private JPanel panelCommand = null;
	private JButton btnThem = null;
	private JButton btnSua = null;
	private JButton btnXoa = null;
	private JTextField txtFilter = null;
	private JLabel jLabel7 = null;
	private JPanel panelPaging = null;
	private JButton btnFirst = null;
	private JButton btnPrevious = null;
	private JLabel lblPage = null;
	private JButton btnNext = null;
	private JButton btnLast = null;

	private JPanel panelChiTietNV = null;

	private JPanel panelImg = null;
	private JLabel jLabel = null;
	private JPanel jPanel = null;
	private JButton btnChange = null;
	private JLabel imgNhanVien = null;

	private ImagePanel panelThongTin = null;

	private JLabel jLabel1 = null;

	private JTextField txtTenNV = null;

	private JLabel jLabel2 = null;

	private NumbericTextField txtCMND = null;

	private JLabel jLabel3 = null;

	private JScrollPane scrollPaneDiaChi = null;

	private JTextArea txtDiaChi = null;

	private JLabel jLabel4 = null;

	private JTextField txtSdt = null;

	private JLabel jLabel5 = null;

	private JTextFieldCurrency txtMucLuong = null;

	private JLabel jLabel6 = null;

	private JScrollPane scrollPaneGhiChu = null;

	private JTextArea txtGhiChu = null;

	private JButton btnChapNhan = null;

	private JButton btnHuy = null;

	/**
	 * This is the xxx default constructor
	 */
	public QuanLyNV() {
		super();
		initialize();
		NhanVien nv = getFirstRecord();
		if (nv != null) {
			fillDataAllFields(nv);
			nvSelected = nv;
		}
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(800, 600);
		this.setContentPane(getJContentPane());
		this.setTitle(Resources_Duy.TITLE_MENU_ITEM_THONGTINNV);
		this.setClosable(true);
		this.setIconifiable(true);
	}

	// ========================== PROCESS METHOD =======================

	private int getSizeData() {
		int size = 0;
		try {
			NhanVienDAO nvDao = new NhanVienDAO();
			size = nvDao.countSizeData();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return size;
	}

	private int getNumberRowOdd() {
		return getSizeData() % MAX_NUMBER_PER_PAGE;
	}

	private NhanVien getFirstRecord() {
		NhanVien nv = null;
		try {
			NhanVienDAO nvDao = new NhanVienDAO();
			nv = nvDao.findByFirstRecord();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Lỗi",
					JOptionPane.ERROR_MESSAGE);
		}
		return nv;
	}

	private NhanVien getNVSelected(int index) {
		NhanVien nv = null;
		try {
			NhanVienDAO nvDao = new NhanVienDAO();
			nv = (NhanVien) nvDao.findByPk(NhanVien.class, (Long
					.parseLong((getTableData().getModel().getValueAt(index, 6))
							.toString())));
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Lỗi",
					JOptionPane.ERROR_MESSAGE);
		}
		return nv;
	}

	private void processThem() {
		resetAllFields();
		nvSelected = null;
	}

	private void processSua() {
		int index = getTableData().getSelectedRow();
		if (index > -1) {
			nvSelected = getNVSelected(index);
			if (nvSelected != null)
				fillDataAllFields(nvSelected);
		} else {
			JOptionPane.showMessageDialog(this, "Bạn Hãy Chọn Nhân Viên !",
					"Thông Báo", JOptionPane.INFORMATION_MESSAGE);
		}
	}

	private void saveObject() {
		NhanVien nv = null;
		String thongBao = "";
		if (nvSelected != null) {
			nv = nvSelected;
			thongBao = "Cập Nhật Thành Công !";
		} else {
			nv = new NhanVien();
			nv.setMaSo(Common.getID());
			thongBao = "Thêm Mới Thành Công !";
		}
		nv.setTen(txtTenNV.getText().trim());
		nv.setCmnd(txtCMND.getText().trim());
		nv.setDiaChi(txtDiaChi.getText().trim());
		nv.setDienThoai(txtSdt.getText().trim());
		nv.setMucLuong(Integer.parseInt(txtMucLuong.getValue().toString()));
		nv.setGhiChu(txtGhiChu.getText());
		if (imgCurrent != null)
			nv.setHinhAnh(nv.getMaSo() + extension);

		NhanVienDAO nvDao = new NhanVienDAO();
		if (nvDao.saveOrUpdate(nv)) {
			JOptionPane.showMessageDialog(this, thongBao, "Thông Báo",
					JOptionPane.INFORMATION_MESSAGE);

			if (imgCurrent != null) {
				File f = new File(Resources_Duy.PATH_SAVE_IMAGE
						+ nv.getHinhAnh());
				if (f.isFile()) {
					f.delete();
				}
				Common.writeImgToFile(imgCurrent, extension.substring(1),
						Resources_Duy.PATH_SAVE_IMAGE + nv.getHinhAnh());
			}

			// refresh table
			if (nvSelected == null) {
				indexPos = (getSizeData() / MAX_NUMBER_PER_PAGE)
						* MAX_NUMBER_PER_PAGE;
				if (getNumberRowOdd() > 0) {
					pagingModel.setIndexRow(indexPos);
					pagingModel.setRowCount(getNumberRowOdd());
				} else {
					indexPos = indexPos - MAX_NUMBER_PER_PAGE;
					pagingModel.setIndexRow(indexPos);
					pagingModel.setRowCount(MAX_NUMBER_PER_PAGE);
				}

				btnNext.setEnabled(false);
				btnLast.setEnabled(false);
				btnPrevious.setEnabled(true);
				btnFirst.setEnabled(true);

				lblPage.setText(getSizeData() + " / " + getSizeData());

			}

			((AbstractTableModel) getTableData().getModel())
					.fireTableDataChanged();
		} else {
			JOptionPane.showMessageDialog(this, "Có Lỗi Xảy Ra !", "Lỗi",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	private void processChangeImage() {
		try {
			Common.tranformJFileChooserToVi();
			JFileChooser fileChooser = new JFileChooser();
			fileChooser.addChoosableFileFilter(new ImageFileFilter());
			fileChooser.setFileView(new ImageFileView());

			int returnVal = fileChooser.showOpenDialog(this);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				File fileImg = fileChooser.getSelectedFile();

				BufferedImage img = Common.getBufferImage(fileImg);

				if (img.getWidth() > 128 || img.getHeight() > 128) {
					JOptionPane.showMessageDialog(this,
							"Kích Thước Hình Ảnh Không Phù Hợp !", "Thông Báo",
							JOptionPane.INFORMATION_MESSAGE);
					return;
				}

				ImageIcon ic = Common.convertBufferImageToImageIcon(img);
				imgNhanVien.setIcon(ic);
				imgCurrent = img;
				extension = Common.getExtension(fileImg.getName());
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Lỗi",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	private void processDelete() {
		try {
			NhanVienDAO nvDao = new NhanVienDAO();
			if (JOptionPane.showConfirmDialog(this,
					"Bạn Có Thực Sự Muốn Xóa Nhân Viên Này Không ?",
					"Thông Báo", JOptionPane.YES_NO_OPTION,
					JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
				int index = getTableData().getSelectedRow();
				if (index > -1) {
					NhanVien nv = getNVSelected(index);
					if (nv != null)
						if (nvDao.delete(nv)) {
							File f = new File(Resources_Duy.PATH_SAVE_IMAGE
									+ nv.getHinhAnh());
							if (f.isFile())
								f.delete();
							JOptionPane.showMessageDialog(this,
									"Đã Xóa Thành Công Nhân Viên "
											+ nv.getTen().toUpperCase(),
									"Thông Báo",
									JOptionPane.INFORMATION_MESSAGE);

							if (getSizeData() > MAX_NUMBER_PER_PAGE) {
								if (getNumberRowOdd() >= 0
										&& indexPos + MAX_NUMBER_PER_PAGE > getSizeData()) {
									pagingModel.setIndexRow(indexPos);
									pagingModel.setRowCount(getNumberRowOdd());
									lblPage.setText(getSizeData() + " / "
											+ getSizeData());
								} else
									lblPage.setText(indexPos
											+ MAX_NUMBER_PER_PAGE + " / "
											+ getSizeData());
								((AbstractTableModel) getTableData().getModel())
										.fireTableDataChanged();
							} else {
								indexPos = 0;
								pagingModel.setIndexRow(indexPos);
								pagingModel.setRowCount(getSizeData());
								lblPage.setText(getSizeData() + " / "
										+ getSizeData());
								if (getSizeData() > 0)
									((AbstractTableModel) getTableData()
											.getModel()).fireTableDataChanged();
							}
						}

					if (getSizeData() <= MAX_NUMBER_PER_PAGE) {
						btnPrevious.setEnabled(false);
						btnFirst.setEnabled(false);
					}
				}
				else {
					JOptionPane.showMessageDialog(this, "Bạn Hãy Chọn Nhân Viên !",
							"Thông Báo", JOptionPane.INFORMATION_MESSAGE);
				}
			}
			
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, "Có Lỗi Xảy Ra !", "Lỗi",
					JOptionPane.ERROR_MESSAGE);
		}

	}

	private void fillDataAllFields(NhanVien nv) {
		txtTenNV.setText(nv.getTen());
		txtCMND.setText(nv.getCmnd());
		txtDiaChi.setText(nv.getDiaChi());
		txtSdt.setText(nv.getDienThoai());
		txtMucLuong.setValue(nv.getMucLuong());
		txtGhiChu.setText(nv.getGhiChu());
		ImageIcon img = null;
		try {
			File f = new File(Resources_Duy.PATH_SAVE_IMAGE + nv.getHinhAnh());
			if (f.isFile()) {
				BufferedImage im = Common.getBufferImage(f);

				img = Common.convertBufferImageToImageIcon(im);
			} else
				img = Common.getImageIcon(Resources_Duy.IMAGE_DEFAULT_AVATAR);
		} catch (NullPointerException e) {
			img = Common.getImageIcon(Resources_Duy.IMAGE_DEFAULT_AVATAR);
		}
		imgNhanVien.setIcon(img);
	}

	private void resetAllFields() {
		txtTenNV.setText("");
		txtTenNV.requestFocus();
		txtCMND.setText("");
		txtDiaChi.setText("");
		txtSdt.setText("");
		txtMucLuong.setText("");
		txtGhiChu.setText("");
		imgNhanVien.setIcon(Common
				.getImageIcon(Resources_Duy.IMAGE_DEFAULT_AVATAR));
		imgCurrent = null;
		extension = "";
	}

	private void locDuLieu() {
		if (Validator.isNotBlank(txtFilter.getText()))
			sorter.setRowFilter(RowFilter.regexFilter(txtFilter.getText()));
		else
			sorter.setRowFilter(null);
	}
	
	// ========================== END PROCESS METHOD ===================

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getPanelDuLieu(), null);
			jContentPane.add(getPanelChiTietNV(), null);
			jContentPane.add(getBtnChapNhan(), null);
			jContentPane.add(getBtnHuy(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes panelDuLieu
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelDuLieu() {
		if (panelDuLieu == null) {
			panelDuLieu = new JPanel();
			panelDuLieu.setLayout(null);
			panelDuLieu.setBounds(new Rectangle(14, 14, 767, 277));
			panelDuLieu.setBorder(BorderFactory
					.createTitledBorder("Danh Sách Bàn"));
			panelDuLieu.add(getPanelPaging(), null);
			panelDuLieu.add(getScrollPaneTable(), null);
			panelDuLieu.add(getPanelCommand(), null);
			panelDuLieu.add(getPanelPaging(), null);

		}
		return panelDuLieu;
	}

	/**
	 * This method initializes scrollPaneTable
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getScrollPaneTable() {
		if (scrollPaneTable == null) {
			scrollPaneTable = new JScrollPane();
			scrollPaneTable.setBounds(new Rectangle(15, 51, 738, 188));
			scrollPaneTable.setViewportView(getTableData());
		}
		return scrollPaneTable;
	}

	/**
	 * This method initializes tableData
	 * 
	 * @return javax.swing.JTable
	 */
	private JTable getTableData() {
		if (tableData == null) {
			tableData = new JTable();

			pagingModel = new QLNVTableModel();
			if (getSizeData() <= MAX_NUMBER_PER_PAGE) {
				getBtnNext().setEnabled(false);
				getBtnLast().setEnabled(false);
				lblPage.setText(getSizeData() + " / " + getSizeData());
				pagingModel.setRowCount(getSizeData());
			} else {
				lblPage.setText(indexPos + MAX_NUMBER_PER_PAGE + " / "
						+ getSizeData());
				pagingModel.setRowCount(MAX_NUMBER_PER_PAGE);
			}
			tableData.setModel(pagingModel);

			// sorter
			sorter = new TableRowSorter<QLNVTableModel>(pagingModel);
			tableData.setRowSorter(sorter);

			tableData.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent me) {
					if (me.getClickCount() == 2) {
						processSua();
					}
				}
			});
			// renderer currency
			TableColumn columnMucLuong = tableData.getColumnModel()
					.getColumn(4);
			columnMucLuong.setCellRenderer(new CurrencyTableCellRenderer());

		}
		return tableData;
	}

	/**
	 * This method initializes panelCommand
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelCommand() {
		if (panelCommand == null) {
			jLabel7 = new JLabel();
			jLabel7.setBounds(new Rectangle(494, 1, 25, 23));
			jLabel7.setText("");
			jLabel7.setIcon(Common.getImageIcon(Resources_Duy.ICON_PATH
					+ "filter.png"));
			panelCommand = new JPanel();
			panelCommand.setLayout(null);
			panelCommand.setBounds(new Rectangle(16, 26, 737, 27));
			panelCommand.add(getBtnThem(), null);
			panelCommand.add(getBtnSua(), null);
			panelCommand.add(getBtnXoa(), null);
			panelCommand.add(getTxtFilter(), null);
			panelCommand.add(jLabel7, null);
		}
		return panelCommand;
	}

	/**
	 * This method initializes btnThem
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnThem() {
		if (btnThem == null) {
			btnThem = new JButton();
			btnThem.setBounds(new Rectangle(0, 0, 90, 26));
			btnThem.setIcon(Common.getImageIcon(Resources_Duy.ICON_PATH
					+ "add_new.png"));
			btnThem.setText("Thêm");
			btnThem.setContentAreaFilled(false);

			btnThem.addMouseListener(new MouseAdapter() {
				public void mouseEntered(MouseEvent event) {
					btnThem.setContentAreaFilled(true);
				}

				public void mouseExited(MouseEvent event) {
					btnThem.setContentAreaFilled(false);
				}
			});
		}

		btnThem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				processThem();
			}
		});
		return btnThem;
	}

	/**
	 * This method initializes btnSua
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnSua() {
		if (btnSua == null) {
			btnSua = new JButton();
			btnSua.setBounds(new Rectangle(90, 0, 85, 26));
			btnSua.setIcon(Common.getImageIcon(Resources_Duy.ICON_PATH
					+ "edit.png"));
			btnSua.setText("Sửa");
			btnSua.setContentAreaFilled(false);

			btnSua.addMouseListener(new MouseAdapter() {
				public void mouseEntered(MouseEvent event) {
					btnSua.setContentAreaFilled(true);
				}

				public void mouseExited(MouseEvent event) {
					btnSua.setContentAreaFilled(false);
				}
			});

			btnSua.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {

					processSua();
				}
			});
		}
		return btnSua;
	}

	/**
	 * This method initializes btnXoa
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnXoa() {
		if (btnXoa == null) {
			btnXoa = new JButton();
			btnXoa.setBounds(new Rectangle(175, 0, 85, 26));
			btnXoa.setIcon(Common.getImageIcon(Resources_Duy.ICON_PATH
					+ "delete.png"));
			btnXoa.setText("Xóa");
			btnXoa.setContentAreaFilled(false);

			btnXoa.addMouseListener(new MouseAdapter() {
				public void mouseEntered(MouseEvent event) {
					btnXoa.setContentAreaFilled(true);
					btnXoa.setBackground(Color.getColor("#C7D3E0"));
				}

				public void mouseExited(MouseEvent event) {
					btnXoa.setContentAreaFilled(false);
				}
			});
			
			btnXoa.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					processDelete();
				}
			});
		}
		return btnXoa;
	}

	/**
	 * This method initializes txtFilter
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtFilter() {
		if (txtFilter == null) {
			txtFilter = new JTextField();
			txtFilter.setBounds(new Rectangle(519, 0, 217, 26));
			
			txtFilter.getDocument().addDocumentListener(new DocumentListener() {

				@Override
				public void removeUpdate(DocumentEvent arg0) {
					// TODO Auto-generated method stub
					locDuLieu();
				}

				@Override
				public void insertUpdate(DocumentEvent arg0) {
					// TODO Auto-generated method stub
					locDuLieu();
				}

				@Override
				public void changedUpdate(DocumentEvent arg0) {
					// TODO Auto-generated method stub
					locDuLieu();
				}
			});
		}
		return txtFilter;
	}

	/**
	 * This method initializes panelPaging
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelPaging() {
		if (panelPaging == null) {
			lblPage = new JLabel();
			lblPage.setBounds(new Rectangle(110, 1, 55, 20));
			lblPage.setText("");
			lblPage.setHorizontalAlignment(SwingConstants.CENTER);
			panelPaging = new JPanel();
			panelPaging.setLayout(null);
			panelPaging.setBounds(new Rectangle(534, 240, 222, 22));
			panelPaging.add(getBtnFirst(), null);
			panelPaging.add(getBtnPrevious(), null);
			panelPaging.add(lblPage, null);
			panelPaging.add(getBtnNext(), null);
			panelPaging.add(getBtnLast(), null);
		}
		return panelPaging;
	}

	/**
	 * This method initializes btnFirst
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnFirst() {
		if (btnFirst == null) {
			btnFirst = new JButton();
			btnFirst.setBounds(new Rectangle(54, 1, 32, 20));
			btnFirst.setContentAreaFilled(false);
			btnFirst.setIcon(Common.getImageIcon(Resources_Duy.ICON_PATH
					+ "first.png"));
			btnFirst.setEnabled(false);

			btnFirst.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					indexPos = 0;
					pagingModel.setRowCount(MAX_NUMBER_PER_PAGE);
					pagingModel.setIndexRow(indexPos);
					lblPage.setText(indexPos + MAX_NUMBER_PER_PAGE + " / "
							+ getSizeData());
					((AbstractTableModel) getTableData().getModel())
							.fireTableDataChanged();
					btnFirst.setEnabled(false);
					btnPrevious.setEnabled(false);
					btnNext.setEnabled(true);
					btnLast.setEnabled(true);
				}
			});
		}
		return btnFirst;
	}

	/**
	 * This method initializes btnPrevious
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnPrevious() {
		if (btnPrevious == null) {
			btnPrevious = new JButton();
			btnPrevious.setBounds(new Rectangle(86, 1, 24, 20));
			btnPrevious.setContentAreaFilled(false);
			btnPrevious.setIcon(Common.getImageIcon(Resources_Duy.ICON_PATH
					+ "previous.png"));
			btnPrevious.setEnabled(false);

			btnPrevious.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					indexPos = indexPos - MAX_NUMBER_PER_PAGE;
					getBtnNext().setEnabled(true);
					getBtnLast().setEnabled(true);

					pagingModel.setRowCount(MAX_NUMBER_PER_PAGE);
					pagingModel.setIndexRow(indexPos);
					if (indexPos < MAX_NUMBER_PER_PAGE) {
						getBtnPrevious().setEnabled(false);
						getBtnFirst().setEnabled(false);
					} else
						getBtnPrevious().setEnabled(true);
					lblPage.setText(indexPos + MAX_NUMBER_PER_PAGE + " / "
							+ getSizeData());
					tableData.tableChanged(new TableModelEvent(tableData
							.getModel()));
				}
			});
		}
		return btnPrevious;
	}

	/**
	 * This method initializes btnNext
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnNext() {
		if (btnNext == null) {
			btnNext = new JButton();
			btnNext.setBounds(new Rectangle(165, 1, 24, 20));
			btnNext.setIcon(Common.getImageIcon(Resources_Duy.ICON_PATH
					+ "next.png"));
			btnNext.setContentAreaFilled(false);

			btnNext.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					indexPos = indexPos + MAX_NUMBER_PER_PAGE;
					btnNext.setEnabled(true);
					btnFirst.setEnabled(true);
					getBtnPrevious().setEnabled(true);
					if (getSizeData() - indexPos >= MAX_NUMBER_PER_PAGE) {
						pagingModel.setRowCount(MAX_NUMBER_PER_PAGE);
						pagingModel.setIndexRow(indexPos);
						if (getSizeData() - indexPos == MAX_NUMBER_PER_PAGE) {
							getBtnNext().setEnabled(false);
							getBtnLast().setEnabled(false);
						}
						lblPage.setText(indexPos + MAX_NUMBER_PER_PAGE + " / "
								+ getSizeData());
					} else {
						btnNext.setEnabled(false);
						btnLast.setEnabled(false);
						pagingModel.setIndexRow(indexPos);
						pagingModel.setRowCount(getNumberRowOdd());
						lblPage.setText(indexPos + getNumberRowOdd() + " / "
								+ getSizeData());
					}

					((AbstractTableModel) getTableData().getModel())
							.fireTableDataChanged();

				}
			});
		}
		return btnNext;
	}

	/**
	 * This method initializes btnLast
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnLast() {
		if (btnLast == null) {
			btnLast = new JButton();
			btnLast.setBounds(new Rectangle(189, 1, 32, 20));
			btnLast.setIcon(Common.getImageIcon(Resources_Duy.ICON_PATH
					+ "last.png"));
			btnLast.setContentAreaFilled(false);

			btnLast.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {

					if (getNumberRowOdd() > 0) {
						indexPos = getSizeData() - getNumberRowOdd();
						pagingModel.setRowCount(getNumberRowOdd());
						lblPage.setText(indexPos + getNumberRowOdd() + " / "
								+ getSizeData());
					} else {
						indexPos = getSizeData() - MAX_NUMBER_PER_PAGE;
						pagingModel.setRowCount(MAX_NUMBER_PER_PAGE);
						lblPage.setText(indexPos + MAX_NUMBER_PER_PAGE + " / "
								+ getSizeData());
					}

					pagingModel.setIndexRow(indexPos);

					((AbstractTableModel) getTableData().getModel())
							.fireTableDataChanged();
					btnFirst.setEnabled(true);
					btnPrevious.setEnabled(true);
					btnLast.setEnabled(false);
					btnNext.setEnabled(false);
				}
			});
		}
		return btnLast;
	}

	/**
	 * This method initializes panelChiTietNV
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelChiTietNV() {
		if (panelChiTietNV == null) {
			panelChiTietNV = new JPanel();
			panelChiTietNV.setLayout(null);
			panelChiTietNV.setBounds(new Rectangle(15, 296, 766, 238));
			panelChiTietNV.setBorder(BorderFactory
					.createTitledBorder("Chi Tiết Nhân Viên"));
			panelChiTietNV.add(getPanelImg(), null);
			panelChiTietNV.add(getPanelThongTin(), null);
		}
		return panelChiTietNV;
	}

	/**
	 * This method initializes panelImg
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPanelImg() {
		if (panelImg == null) {
			imgNhanVien = new JLabel();
			imgNhanVien.setBounds(new Rectangle(10, 31, 128, 128));
			imgNhanVien.setText("");
			imgNhanVien.setIcon(Common
					.getImageIcon(Resources_Duy.IMAGE_DEFAULT_AVATAR));
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(0, 0, 150, 25));
			jLabel.setText("Hình Ảnh");
			jLabel.setFont(jLabel.getFont().deriveFont(Font.BOLD));
			jLabel.setHorizontalAlignment(SwingConstants.CENTER);
			jLabel.setOpaque(true);
			jLabel.setBackground(Resources_Duy.COLOR_BG_TITLE_IMAGE);
			jLabel.setForeground(Color.WHITE);
			panelImg = new JPanel();
			panelImg.setLayout(null);
			panelImg.setBounds(new Rectangle(16, 30, 150, 190));
			Border borderLine = BorderFactory.createLineBorder(new Color(77,
					122, 153));
			panelImg.setBorder(borderLine);
			panelImg.add(jLabel, null);
			panelImg.add(getJPanel(), null);
			panelImg.add(imgNhanVien, null);
		}
		return panelImg;
	}

	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.setBounds(new Rectangle(0, 164, 150, 25));
			jPanel.setOpaque(true);
			jPanel.setBackground(new Color(77, 122, 153));
			jPanel.add(getBtnChange(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes btnChange
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnChange() {
		if (btnChange == null) {
			btnChange = new JButton();
			btnChange.setBounds(new Rectangle(33, 0, 88, 25));
			btnChange.setText("Thay Đổi");
			btnChange.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					processChangeImage();
				}
			});
		}
		return btnChange;
	}

	/**
	 * This method initializes panelThongTin
	 * 
	 * @return javax.swing.JPanel
	 */
	private ImagePanel getPanelThongTin() {
		if (panelThongTin == null) {
			jLabel6 = new JLabel();
			jLabel6.setBounds(new Rectangle(0, 101, 90, 25));
			jLabel6.setText(" Ghi Chú");
			jLabel6.setFont(jLabel6.getFont().deriveFont(Font.BOLD));
			jLabel5 = new JLabel();
			jLabel5.setBounds(new Rectangle(286, 76, 86, 25));
			jLabel5.setText(" Mức Lương");
			jLabel5.setFont(jLabel5.getFont().deriveFont(Font.BOLD));
			jLabel4 = new JLabel();
			jLabel4.setBounds(new Rectangle(0, 76, 91, 25));
			jLabel4.setText(" Số Điện Thoại");
			jLabel4.setFont(jLabel4.getFont().deriveFont(Font.BOLD));
			jLabel3 = new JLabel();
			jLabel3.setBounds(new Rectangle(0, 25, 86, 25));
			jLabel3.setText(" Địa Chỉ");
			jLabel3.setFont(jLabel3.getFont().deriveFont(Font.BOLD));
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(286, 0, 63, 24));
			jLabel2.setText(" CMND");
			jLabel2.setFont(jLabel2.getFont().deriveFont(Font.BOLD));
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(1, 1, 88, 23));
			jLabel1.setText(" Tên Nhân Viên");
			jLabel1.setFont(jLabel1.getFont().deriveFont(Font.BOLD));
			panelThongTin = new ImagePanel(Resources_Duy.IMAGE_PATH
					+ "bgQLNV.png");
			panelThongTin.setLayout(null);
			panelThongTin.setBounds(new Rectangle(179, 30, 570, 190));
			panelThongTin.add(jLabel1, null);
			panelThongTin.add(getTxtTenNV(), null);
			panelThongTin.add(jLabel2, null);
			panelThongTin.add(getTxtCMND(), null);
			panelThongTin.add(jLabel3, null);
			panelThongTin.add(getScrollPaneDiaChi(), null);
			panelThongTin.add(jLabel4, null);
			panelThongTin.add(getTxtSdt(), null);
			panelThongTin.add(jLabel5, null);
			panelThongTin.add(getTxtMucLuong(), null);
			panelThongTin.add(jLabel6, null);
			panelThongTin.add(getScrollPaneGhiChu(), null);
		}
		return panelThongTin;
	}

	/**
	 * This method initializes txtTenNV
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtTenNV() {
		if (txtTenNV == null) {
			txtTenNV = new JTextField();
			txtTenNV.setBounds(new Rectangle(101, 0, 185, 26));
		}
		return txtTenNV;
	}

	/**
	 * This method initializes txtCMND
	 * 
	 * @return javax.swing.JTextField
	 */
	private NumbericTextField getTxtCMND() {
		if (txtCMND == null) {
			txtCMND = new NumbericTextField("0", 0);
			txtCMND.setBounds(new Rectangle(386, 0, 185, 26));
		}
		return txtCMND;
	}

	/**
	 * This method initializes scrollPaneDiaChi
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getScrollPaneDiaChi() {
		if (scrollPaneDiaChi == null) {
			scrollPaneDiaChi = new JScrollPane();
			scrollPaneDiaChi.setBounds(new Rectangle(101, 25, 470, 52));
			scrollPaneDiaChi.setViewportView(getTxtDiaChi());
		}
		return scrollPaneDiaChi;
	}

	/**
	 * This method initializes txtDiaChi
	 * 
	 * @return javax.swing.JTextArea
	 */
	private JTextArea getTxtDiaChi() {
		if (txtDiaChi == null) {
			txtDiaChi = new JTextArea();
		}
		return txtDiaChi;
	}

	/**
	 * This method initializes txtSdt
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtSdt() {
		if (txtSdt == null) {
			txtSdt = new JTextField();
			txtSdt.setBounds(new Rectangle(101, 76, 185, 26));
		}
		return txtSdt;
	}

	/**
	 * This method initializes txtMucLuong
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextFieldCurrency getTxtMucLuong() {
		if (txtMucLuong == null) {
			txtMucLuong = new JTextFieldCurrency();
			txtMucLuong.setBounds(new Rectangle(386, 76, 185, 26));

		}
		return txtMucLuong;
	}

	/**
	 * This method initializes scrollPaneGhiChu
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getScrollPaneGhiChu() {
		if (scrollPaneGhiChu == null) {
			scrollPaneGhiChu = new JScrollPane();
			scrollPaneGhiChu.setBounds(new Rectangle(101, 101, 470, 52));
			scrollPaneGhiChu.setViewportView(getTxtGhiChu());
		}
		return scrollPaneGhiChu;
	}

	/**
	 * This method initializes txtGhiChu
	 * 
	 * @return javax.swing.JTextArea
	 */
	private JTextArea getTxtGhiChu() {
		if (txtGhiChu == null) {
			txtGhiChu = new JTextArea();
		}
		return txtGhiChu;
	}

	/**
	 * This method initializes btnChapNhan
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnChapNhan() {
		if (btnChapNhan == null) {
			btnChapNhan = new JButton();
			btnChapNhan.setText("Chấp Nhận");
			btnChapNhan.setBounds(new Rectangle(580, 533, 99, 25));
			btnChapNhan.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					saveObject();
				}
			});
		}
		return btnChapNhan;
	}

	/**
	 * This method initializes btnHuy
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtnHuy() {
		if (btnHuy == null) {
			btnHuy = new JButton();
			btnHuy.setText("Hủy");
			btnHuy.setBounds(new Rectangle(679, 533, 99, 25));
		}
		return btnHuy;
	}

}
